// pages/testmode/testmode.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    List: [],
    index: 0,
    Options: [], //ABCD
    result: [], //被选中的选项
    time: '02:00', //倒计时
    value: 100,
    timeSum: 120,
    clearTime: function () {},
    gradientColor: { //渐变色
      '0%': '#ffd01e',
      '100%': '#ee0a24',
    },
    resultArr: {}, //存放用户选中的答案
    isShow: false,
  },

  getTestList() {
    wx.request({
      url: 'http://localhost:3001/questions/randList',
      data: {},
      success: (res) => {
        for (let i = 0; i < res.data.data.length; i++) {
          res.data.data[i].answers = JSON.parse(res.data.data[i].answers).sort().join('') //答案转换字符串
          res.data.data[i].typeClass = 'Item' //添加默认颜色
          res.data.data[i].typeId = i //添加唯一id后续根据index判断
        }
        this.setData({
          List: res.data.data,
          Options: JSON.parse(res.data.data[this.data.index].options),
        })
      }
    })
  },
  onChange(event) { //选择按钮事件
    this.setData({
      result: event.detail,
    });
  },
  LastQuestion() { //上一题
    let that = this.data.index;

    if (that == 0) { //判断是否为第一道题
      Dialog.alert({
        title: '温馨提示',
        message: '已经是第一道题啦',
      }).then(() => {});
      return
    }

    if (this.data.result) { //把当前答案存放到用户对象中.
      this.data.resultArr[that + 1] = this.data.result.sort().join('')
      this.setData({
        resultArr: this.data.resultArr
      })
    }

    if (this.data.resultArr[that + 1] == '') { //如果当前用户对象中没有值  让他为默认
      this.data.List[that].typeClass = 'Item'
      this.data.resultArr[that + 1] = ''
      this.setData({
        List: this.data.List,
        resultArr: this.data.resultArr
      })
    }

    if (this.data.resultArr[that + 1]) { //如果有值进行判断
      this.data.List[that].answers == this.data.resultArr[that + 1] ? this.data.List[that].typeClass = 'ok' :
        this.data.List[that].typeClass = 'wrong';
      this.setData({
        List: this.data.List
      })
    }

    console.log(this.data.resultArr);
    this.setData({
      index: that - 1,
      Options: JSON.parse(this.data.List[that - 1].options),
      result: this.data.resultArr[that] ? this.data.resultArr[that].split('') : [], //获取上一题的答案
    })
  },

  NextQuestion() { //下一题
    if (this.data.index == this.data.List.length - 1) { //判断是否为最后一道题
      Dialog.alert({
        title: '温馨提示',
        message: '已经是最后一道题啦',
      }).then(() => {});
      return
    }

    this.setData({ //展示下一题数据
      index: this.data.index + 1,
      Options: JSON.parse(this.data.List[this.data.index + 1].options),
    })



    if (this.data.resultArr[this.data.index + 1]) { //判断Arr某一项是否存在
      this.data.resultArr[this.data.index] = this.data.result ? this.data.result.sort().join('') : this.data.result //继续赋值
      this.setData({ //点击下一题 获取当前索引的答案并赋值
        result: this.data.resultArr[this.data.index + 1].split(''),
      })
    } else {
      this.data.resultArr[this.data.index] = this.data.result ? this.data.result.sort().join('') : this.data.result
      this.setData({
        result: [] //清空原答案
      })
    }
    this.anwswerIsFn() //判断答案
    console.log('用户的', this.data.resultArr, '后台的' + this.data.List[this.data.index - 1].answers);
  },

  Handpapers() { //处理交卷答案
    let num = 0,
      that = this.data.index;
    console.log(this.data.resultArr);
    this.data.result.sort().join('') == this.data.List[that].answers ?
      (this.data.List[that].typeClass = 'ok') :
      this.data.List[that].typeClass = 'wrong'

    this.setData({
      List: this.data.List
    })

    this.data.List.map((v, i) => {
      if (v.typeClass == 'ok') {
        return num++
      }
    });
    let Score = 100 / this.data.List.length,
      Totalscore = Score * num;
    Dialog.alert({
      title: '温馨提示',
      message: '恭喜您,总分数为' + Totalscore + '分',
    }).then(() => {});
  },

  questionIsFn(e) { //下面点击题目赋值
    this.setData({
      index: e.target.dataset.question.typeId,
      Options: JSON.parse(e.target.dataset.question.options),
    })
    if (this.data.resultArr[this.data.index + 1]) {
      this.setData({
        result: this.data.resultArr[e.target.dataset.question.typeId + 1].split('')
      })
    } else {
      this.setData({
        result: []
      })
    }
  },

  anwswerIsFn() { //判断答案
    let that = this.data.index;
    if (this.data.resultArr[that] == "") {
      this.data.List[that - 1].typeClass = 'Item'
      this.setData({
        List: this.data.List
      })
      return //如果没有值就不让它再进行下面的判断
    }
    this.data.List[that - 1].answers == this.data.resultArr[that] ? this.data.List[that - 1].typeClass = 'ok' :
      this.data.List[that - 1].typeClass = 'wrong';
    this.setData({
      List: this.data.List
    })
  },

  start() { //倒计时
    let s = this.data.timeSum; //总数
    this.data.clearTime = setInterval(() => {
      let seconds = this.data.timeSum - 1;
      let time = `${ Math.floor (seconds/60) < 10 ? '0' + Math.floor (seconds/60):Math.floor (seconds/60) }  : ${ seconds % 60 < 10? '0' + seconds % 60 : seconds % 60}`;
      this.setData({
        value: Math.round((seconds / s) * 100),
        time,
        timeSum: seconds
      })
      if (seconds == 0) {
        clearInterval(this.data.clearTime);
        Dialog.alert({
          title: '温馨提示',
          message: '时间到了,快交卷哦！！！',
        }).then(() => {});
      }
    }, 1000)
  },

  anSwerData() { //打开弹出框
    this.setData({
      isShow: true
    })
  },
  onClose() { //关闭弹出框
    this.setData({
      isShow: false
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.getTestList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.start(); //倒计时
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.clearTime);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})